<?php


class PlacesController extends AppController{
     var $uses = array('Place', 'PlaceCity', 'PlaceCountry', 'PlaceDepartment', 'PlaceState');

     public $layout = 'place';
     
    public function index(){
        $villou = $this->PlaceCity->find('all');
        $this->set('villou', $villou);
        if (!empty($this->request->data)){
            $data = $this->request->data;
            $val = $data['Place']['content'];
            
            $results = $this->searchPlaces($val);
            $this->set('results', $results);
        }
    }
    
    /**
     *
     * @param type $val 
     */
    public function searchPlaces($val, $where = array(
        'PlaceCountry','PlaceState', 'PlaceDepartment', 'PlaceCity'
    )){
        $data = array();
        $condition = '%'.$val.'%';
        foreach ($where as $place){
            $data[$place] = $this->$place->find('all', array(
                'conditions' => array(
                    $place.'.name LIKE' => $condition
                )
            ));
            echo $place;
        }
        return $data;
    }
    
    /*
     * @param $id -> Id d'une place
     * @param $where
     * @return liste de place 
     * 
     * Exemple :
     * $id = Rhône-Alpes
     * $where = PlaceDepartment
     * retourne la liste des départements de Rhônes-Alpes
     * 
    */
    public function search ($id,$where = array(
        'PlaceCountry','PlaceState', 'PlaceDepartment', 'World'))
    {
        if ($where == 'World') {
            $data = $this->PlaceCountry->find('all');
        }
        else if($where == 'PlaceCountry'){
            $data = $this->PlaceState->find('all',array(
                'conditions' => array(
                    'PlaceState.countries_id' => $id
                )
            ));
        }
        else if($where == 'PlaceState'){
            $data = $this->PlaceDepartment->find('all',array(
                'conditions' => array(
                    'PlaceDepartment.states_id' => $id
                )
            ));
        }
        else if ($where == 'PlaceDepartment'){
            $data = $this->PlaceCity->find('all',array(
                'conditions' => array(
                    'PlaceCity.departments_id' => $id
                )
            ));
        }
        else 
            $data = NULL;
        
        return $data;
    }
    
    public function admin_index(){
        $this->layout = 'admin';
    }
    
    
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('index');
    }
    
    public function admin_del($data){
        $params = $this->request->params['named'];
        $id = $params['id'];
        $this->PlaceCountry->delete($id);
        $this->PlaceState->delete($id);
        $this->PlaceDepartment->delete($id);
        $this->PlaceCity->delete($id);
        $this->Place->delete($id);
        
        if (!isset($params['param'])){
            $params['param'] = null;
        }
        $this->redirect(array(
            'controller' => $params['controllerName'],
            'action' => $params['actionName'],
            $params['param']
        ));
        
    }
    
    /**
     * Vue permettant d'afficher n'importe quelle type de lieu (Ville, départment, région, Pays..)
     * Accessible pour n'importe quelle utilisateur connecté
     * Affiche pour un lieu : 
     *  => Les actions en rapport avec ce lieux
     *  => ??
     */
    public function view($id){
        
        $data = $this->Place->find('first', array(
            'conditions' => array(
                'Place.id' => $id
            )
        ));
        
        $type = $data['Place']['type'];
        $model = 'Place'.$type;
        $data['Place']['model'] = $model;
        
        $data[$model] = $this->$model->find('first', array(
            'conditions' => array(
                'Place.id' => $id
            )
        ));
        
        
        $this->set('place', $data);
        
    }
    
}
?>
